package com.mark.web.data.util;



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: Cookie工具类
 * @Author: Mark
 * @CreateDate: 2018/4/21.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Slf4j
public class CookieUtil {
    private final static String COOKIE_DOMAIN = ".choisaaaa.campuso2o.com";
    private final static String COOKIE_NAME = "login_token";
    private final static String COOKIE_ADMIN_NAME = "admin_login_token";

    //往response中写入Cookie
    public  static void writeLoginToken(HttpServletResponse response, String cookieValue, HttpServletRequest request){
        Cookie cookie = new Cookie(COOKIE_NAME,cookieValue);
        // TODO: 2018/8/13 domain的设置 
        //cookie.setDomain(COOKIE_DOMAIN);
        cookie.setPath("/");//设置在根目录
        cookie.setHttpOnly(true);
        //如果这个maxAge不设置的话，cookie就不会写入硬盘，而是写在内存。只在当前页面有效
        cookie.setMaxAge(60*60*24*365);//如果是-1代表永久
        response.addCookie(cookie);
    }

    //從request中读取Cookie
    public static String readLoginToken(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie: cookies){
               // log.info("read cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
                if(StringUtils.equals(cookie.getName(),COOKIE_NAME)){
                   // log.info("return cookie name:{},cookie value{}",cookie.getName(),cookie.getValue());
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
    public static String readAdminLoginToken(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie: cookies){
                log.info("read cookieName:{},cookieValue:{}",cookie.getName(),cookie.getValue());
                if(StringUtils.equals(cookie.getName(),COOKIE_ADMIN_NAME)){
                    log.info("return cookie name:{},cookie value{}",cookie.getName(),cookie.getValue());
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    //删除Cookie
    public static  void delLoginToken(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                if(StringUtils.equals(COOKIE_NAME,cookie.getName())){
                   // cookie.setDomain(COOKIE_DOMAIN);
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    log.info("delete cookie name:{},cookie value :{}",cookie.getName(),cookie.getValue());
                    response.addCookie(cookie);
                    return;
                }
            }
        }
    }

    public static void writeAdminLoginToken(HttpServletResponse httpServletResponse, String cookieValue, HttpServletRequest httpServletRequest) {
        Cookie cookie = new Cookie(COOKIE_ADMIN_NAME,cookieValue);
        cookie.setDomain(COOKIE_DOMAIN);
        cookie.setPath("/");//设置在根目录
        cookie.setHttpOnly(true);
        //如果这个maxAge不设置的话，cookie就不会写入硬盘，而是写在内存。只在当前页面有效
        cookie.setMaxAge(60*60*24*365);//如果是-1代表永久
        httpServletResponse.addCookie(cookie);
    }
}
